<%@page import="classes.SubjectManager"%>
<%@page import="classes.AccountManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.lang.reflect.Array"%>
<%@page import="classes.Group"%>
<%@page import="classes.GroupManager"%>
<%@page import="classes.User"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> <%=request.getParameter("subject") %> Groups
</title>
</head>
<body>
	<h1>Groups:</h1>

	<%
		SubjectManager smanager = (SubjectManager) getServletContext()
				.getAttribute(SubjectManager.SUBJECTMANAGER);
		int lecturer_id = Integer.parseInt(request.getParameter("id"));
		String subject_name = request.getParameter("subject");
		GroupManager gmanager = (GroupManager) getServletContext()
				.getAttribute(GroupManager.GROUPMANAGER);
		int subject_id = smanager.getSubjectId(subject_name);
		if (request.getParameter("delete") != null) {
			gmanager.deleteGroup(Integer.parseInt(request
					.getParameter("group_id")));
		}
		if (request.getParameter("bool") != null) {
			String group_name = request.getParameter("groupName");
			if (!group_name.equals("")) {
				gmanager.createGroup(request.getParameter("groupName"),
						lecturer_id, subject_id);
			}
		}
		if (request.getParameter("rename") != null) {
			int group_id = Integer.parseInt(request
					.getParameter("group_id"));
			String new_group_name = request.getParameter("newGroupName");
			if (!new_group_name.equals("")) {
				gmanager.renameGroup(group_id, new_group_name);
			}
		}
		ArrayList<String> groupNames = gmanager.getGroupNames(subject_id,
				lecturer_id);
		for (int i = 0; i < groupNames.size(); i++) {
			out.println("<ul><li><h2><a href=\"homeworks.jsp?groupName="
					+ groupNames.get(i) 
					+ "&subject="
					+ subject_name
					+ "\">" 
					+ groupNames.get(i)
					+ "</a><h2></li>");
			out.println("<h5><a href=\"groupStudents.jsp?groupName="
					+ groupNames.get(i) + "&subject="
					+ request.getParameter("subject") + "\">edit</a></h5>");
			out.println("</ul>");
		}
	%>
	<form action="groupsForLectures.jsp" method="post">
		<p>
			Group Name: <input type="text" name=groupName value="">
		</p>
		<input type="submit" name=butt value="Create Group"> <INPUT
			type="hidden" value="<%=request.getParameter("subject")%>"
			name="subject"> <INPUT type="hidden"
			value="<%=request.getParameter("id")%>" name="id"> <INPUT
			type="hidden" value="<%=""%>" name="bool">
			<INPUT
			type="hidden" value="<%=subject_id%>" name="subject_id">
	</form>
	<form action="lecturerLogin.jsp" method="post">
		<input type="submit" name=butt value="Go Back">
	</form>
</body>
</html>